package com.java.auth.utils;

import com.java.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {

    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList){
        //1.创建最终结果返回集合，并查询菜单入口id
        List<SysMenu> res = new ArrayList<>();

        for(SysMenu sysMenu: sysMenuList){
            //入口
            if(sysMenu.getParentId() == 0){
                //得到id，按照id依次查询孩子集合
                getChildList(sysMenu,sysMenuList);
                res.add(sysMenu);
            }
        }
        return res;
    }

    public static void getChildList(SysMenu sysMenu,List<SysMenu> sysMenuList){

        Long id = sysMenu.getId();
        //封装子菜单
        List<SysMenu> childList = new ArrayList<>();

        for(SysMenu menu:sysMenuList){
            //查询子菜单id
            if(menu.getParentId() == id){
                childList.add(menu);
                getChildList(menu,sysMenuList);
            }
        }
        sysMenu.setChildren(childList);
    }
}
